package cn.vc.content.service.video;

import cn.vc.content.entity.video.VideoCollect;
import cn.vc.content.mapper.video.VideoCollectMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class VideoCollectService {
    @Resource(type = VideoCollectMapper.class)
    private VideoCollectMapper videoCollectMapper;



    public boolean collect(Long id, Long userId) {
        VideoCollect videoCollect = new VideoCollect();
        videoCollect.setVideoId(id);
        videoCollect.setUserId(userId);
        try {
            videoCollectMapper.insert(videoCollect);
            return true;
        } catch (Exception e) {
            log.debug("已经收藏了："+userId+" -> "+id);
            this.noCollect(id, userId);
            return false;
        }
    }


    public int noCollect(Long id, Long userId) {
        LambdaQueryWrapper<VideoCollect> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoCollect::getVideoId, id).eq(VideoCollect::getUserId, userId);
        VideoCollect videoCollect = videoCollectMapper.selectOne(lqw);
        if (videoCollect == null) return -1;
        return videoCollectMapper.deleteById(videoCollect.getId());
    }
}
